//
//  LXTabbar.swift
//  weibo
//
//  Created by lixin on 16/4/16.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

protocol LXTabbarDelegate: NSObjectProtocol {
    
    func composeBtnClick()
}

class LXTabbar: UITabBar {

    weak var LXDelegate: LXTabbarDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func composeBtnClick(){
    
        LXDelegate?.composeBtnClick()
    }
    
    func setUI(){
    
        addSubview(composeBtn)
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let width = SCREENW/5
        
        var index:CGFloat = 0
        
        for value in self.subviews{
        
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                value.frame.origin.x = index * width
                
                value.frame.size.width = width
                
                index++
                
                if index == 2 {
                    
                    index++
                }
            }
        }
        
        composeBtn.center.x = self.frame.width/2
        
        composeBtn.center.y = self.frame.height/2
    }
    
    private lazy var composeBtn:UIButton = {
    
        let btn = UIButton()
        
        btn.addTarget(self, action: #selector(LXTabbar.composeBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.sizeToFit()
        
        return btn
    }()
}
